package com.example.weight.greenSeasons.SolidLiquid;
import com.example.weight.R;
import com.example.weight.greenSeasons.compost.duifeiguzhangjilu;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.config.Config;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class guzhangjilu7_7 extends AppCompatActivity {

    ////时间选择
    private EditText editDateStop;
    private EditText editTextDateStart;
    private EditText editTextDateEnd;
    private EditText editDateRestore;

    //设备名称
    //private EditText equipmentName;
    private String systemNameText;
    private Integer faultId;
    //操作人
    private String name, factory;
    private EditText operator;
    //故障信息
    private EditText faultContent;
    //维修负责人
    private EditText servicemanInCharge;
    //维修人
    private EditText serviceman;
    //故障原因
    private EditText faultReason;
    //维修方法
    private EditText serviceMethod;
    //提出人
    private EditText proposer;
    //检查人
    private EditText inspector;

    //填表当前日期
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
    Date incomingDate = new Date(System.currentTimeMillis());
    private String nowDate=simpleDateFormat.format(incomingDate);
    private TextView testid;


    DateFormat format= DateFormat.getDateTimeInstance();
    Calendar calendar= Calendar.getInstance(Locale.CHINA);

    private TitleBar titleBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guzhangjilu7_7);

/*
        //测试
        testid=findViewById(R.id.testid);
        testid.setText(nowDate);*/

        String[] guyefenli_equipment = getResources().getStringArray(R.array.guyefenli_equipment);
        //页面返回按钮
        titleBar=findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                guzhangjilu7_7.this.finish();
            }
            @Override
            public void onTitleClick(View v) {
            }
            @Override
            public void onRightClick(View v) {
            }
        });

        //时间选择
        editDateStop=findViewById(R.id.compost_fault_stopTime);
        editTextDateStart=findViewById(R.id.compost_fault_startTime);
        editTextDateEnd=findViewById(R.id.compost_fault_endTime);
        editDateRestore=findViewById(R.id.compost_fault_restoreTime);
        //设备名称
        //equipmentName=findViewById(R.id.equipmentName);
        Spinner systemName = findViewById(R.id.compost_fault_sysName);

        //操作人
        //用户和职责
        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        factory = previousIntent.getStringExtra("factory");

        operator=findViewById(R.id.operator);
        operator.setText(name);
        operator.setFocusableInTouchMode(false);
        //故障信息
        faultContent=findViewById(R.id.faultContent);
        //维修负责人
        servicemanInCharge=findViewById(R.id.servicemanInCharge);
        //维修人
        serviceman=findViewById(R.id.serviceman);
        //故障原因
        faultReason=findViewById(R.id.faultReason);
        //维修方法
        serviceMethod=findViewById(R.id.serviceMethod);
        //提出人
        proposer=findViewById(R.id.proposer);
        //检查人
        inspector=findViewById(R.id.inspector);

        systemName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 前端设置spinner内容 -> 设备名称
                systemNameText = guyefenli_equipment[position];

                // 获取故障信息
                RequestQueue queue = Volley.newRequestQueue(guzhangjilu7_7.this);
                String url = Config.url + "/sjq/SolidliquidProduceFault/getLastByEquipmentName";
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (!jsonObject.isNull("data")) {
                                        JSONObject data = jsonObject.getJSONObject("data");
                                        faultId = data.getInt("id");
                                        String faultTime = data.getString("faultTime");
                                        String fauContent = data.getString("faultContent");
                                        editDateStop.setText(faultTime);
                                        faultContent.setText(fauContent);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(guzhangjilu7_7.this,
                                "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> map = new HashMap<>();
                        map.put("equipmentName", systemNameText);
                        map.put("factory", factory);
                        return map;
                    }
                };
                stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                queue.add(stringRequest);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        editDateStop.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH = calendar.get(Calendar.MONTH);
                int DAY = calendar.get(Calendar.DAY_OF_MONTH);
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                DatePickerDialog datePickerDialog = new DatePickerDialog(guzhangjilu7_7.this, 2, (view1, year, month, day) -> {
                    // 更新EditText控件日期 小于10加0
                    TimePickerDialog timePickerDialog = new TimePickerDialog(guzhangjilu7_7.this, 2, (view11, hourOfDay, minute) -> {
                        //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                        editDateStop.setText(new StringBuilder()
                                .append(year)
                                .append("/")
                                .append((month + 1) < 10 ? "0"
                                        + (month + 1) : (month + 1))
                                .append("/")
                                .append((day < 10) ? "0" + day : day)
                                .append(" ")
                                .append(String.format("%02d",hourOfDay))
                                .append(":")
                                .append(String.format("%02d",minute)));
                    }, HOUR, MINUTE, true);
                    timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            //取消操作
                            editDateStop.clearFocus();
                        }
                    });
                    timePickerDialog.show();
                }, YEAR, MONTH, DAY);
                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editDateStop.clearFocus();
                    }
                });
                datePickerDialog.show();
            }
        });
        editTextDateStart.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH = calendar.get(Calendar.MONTH);
                int DAY = calendar.get(Calendar.DAY_OF_MONTH);
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                DatePickerDialog datePickerDialog = new DatePickerDialog(guzhangjilu7_7.this, 2, (view1, year, month, day) -> {
                    // 更新EditText控件日期 小于10加0
                    TimePickerDialog timePickerDialog = new TimePickerDialog(guzhangjilu7_7.this, 2, (view11, hourOfDay, minute) -> {
                        //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                        editTextDateStart.setText(new StringBuilder()
                                .append(year)
                                .append("/")
                                .append((month + 1) < 10 ? "0"
                                        + (month + 1) : (month + 1))
                                .append("/")
                                .append((day < 10) ? "0" + day : day)
                                .append(" ")
                                .append(String.format("%02d",hourOfDay))
                                .append(":")
                                .append(String.format("%02d",minute)));
                    }, HOUR, MINUTE, true);
                    timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            //取消操作
                            editTextDateStart.clearFocus();
                        }
                    });
                    timePickerDialog.show();
                }, YEAR, MONTH, DAY);
                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editTextDateStart.clearFocus();
                    }
                });
                datePickerDialog.show();
            }
        });
        editTextDateEnd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH = calendar.get(Calendar.MONTH);
                int DAY = calendar.get(Calendar.DAY_OF_MONTH);
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                DatePickerDialog datePickerDialog = new DatePickerDialog(guzhangjilu7_7.this, 2, (view1, year, month, day) -> {
                    // 更新EditText控件日期 小于10加0
                    TimePickerDialog timePickerDialog = new TimePickerDialog(guzhangjilu7_7.this, 2, (view11, hourOfDay, minute) -> {
                        //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                        editTextDateEnd.setText(new StringBuilder()
                                .append(year)
                                .append("/")
                                .append((month + 1) < 10 ? "0"
                                        + (month + 1) : (month + 1))
                                .append("/")
                                .append((day < 10) ? "0" + day : day)
                                .append(" ")
                                .append(String.format("%02d",hourOfDay))
                                .append(":")
                                .append(String.format("%02d",minute)));
                    }, HOUR, MINUTE, true);
                    timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            //取消操作
                            editTextDateEnd.clearFocus();
                        }
                    });
                    timePickerDialog.show();
                }, YEAR, MONTH, DAY);
                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editTextDateEnd.clearFocus();
                    }
                });
                datePickerDialog.show();
            }
        });
        editDateRestore.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH = calendar.get(Calendar.MONTH);
                int DAY = calendar.get(Calendar.DAY_OF_MONTH);
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                DatePickerDialog datePickerDialog = new DatePickerDialog(guzhangjilu7_7.this, 2, (view1, year, month, day) -> {
                    // 更新EditText控件日期 小于10加0
                    TimePickerDialog timePickerDialog = new TimePickerDialog(guzhangjilu7_7.this, 2, (view11, hourOfDay, minute) -> {
                        //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                        editDateRestore.setText(new StringBuilder()
                                .append(year)
                                .append("/")
                                .append((month + 1) < 10 ? "0"
                                        + (month + 1) : (month + 1))
                                .append("/")
                                .append((day < 10) ? "0" + day : day)
                                .append(" ")
                                .append(String.format("%02d",hourOfDay))
                                .append(":")
                                .append(String.format("%02d",minute)));
                    }, HOUR, MINUTE, true);
                    timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            //取消操作
                            editDateRestore.clearFocus();
                        }
                    });
                    timePickerDialog.show();
                }, YEAR, MONTH, DAY);
                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editDateRestore.clearFocus();
                    }
                });
                datePickerDialog.show();
            }
        });


        /*
        //获取当天最新一条数据
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/sjq/SolidliquidProduceFault/getlast";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONObject data;
                            if(jsonObject.get("data")!=null && !jsonObject.get("data").toString().equals("null")){
                                data = jsonObject.getJSONObject("data");
                                if (!data.isNull("id")) {
                                    //equipmentName.setText(data.getString("equipmentName"));
                                    operator.setText(data.getString("operator"));
                                    faultContent.setText(data.getString("faultContent"));
                                    servicemanInCharge.setText(data.getString("servicemanInCharge"));
                                    serviceman.setText(data.getString("serviceman"));
                                    faultReason.setText(data.getString("faultReason"));
                                    serviceMethod.setText(data.getString("serviceMethod"));
                                    proposer.setText(data.getString("proposer"));
                                    inspector.setText(data.getString("inspector"));
                                    editDateStop.setText(data.getString("faultTime"));
                                    editTextDateStart.setText(data.getString("serviceTimeStart"));
                                    editTextDateEnd.setText(data.getString("serviceTimeEnd"));
                                    editDateRestore.setText(data.getString("recoverTime"));
                                    Toast.makeText(guzhangjilu7_7.this,
                                            "今日已填写此表", Toast.LENGTH_SHORT).show();
                                }
                            }else{
                                editDateStop.setHint("2020-01-01 00:00");
                                editTextDateStart.setHint("2020-01-01 00:00");
                                editTextDateEnd.setHint("2020-01-01 00:00");
                                editDateRestore.setHint("2020-01-01 00:00");
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(guzhangjilu7_7.this,
                        "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);*/

    }


    public void save(View view) {
        if (systemNameText == null) {
            Toast.makeText(this, "请选择故障系统", Toast.LENGTH_SHORT).show();
            return;
        }
        new AlertDialog.Builder(this)
                .setTitle("确定提交吗？")
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) ->
                        Toast.makeText(this, "取消提交", Toast.LENGTH_SHORT).show()))
                .setPositiveButton("确定", (dialog, which) -> {
                    // 提交表单
                    RequestQueue queue = Volley.newRequestQueue(this);
                    if (faultId == null) {
                        // 填写前半部分信息
                        if(editDateStop.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写停机时间", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (faultContent.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写故障情况描述", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        String url = Config.url + "/sjq/SolidliquidProduceFault/addFrontPart";
                        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            String code = jsonObject.getString("code");

                                            if (code.startsWith("2"))
                                                Toast.makeText(guzhangjilu7_7.this,
                                                        "提交成功", Toast.LENGTH_SHORT).show();
                                            else
                                                Toast.makeText(guzhangjilu7_7.this,
                                                        "提交失败", Toast.LENGTH_SHORT).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(guzhangjilu7_7.this,
                                        "提交失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() {
                                Map<String, String> map=new HashMap<>();
                                map.put("equipmentName", systemNameText);
                                map.put("faultTime", editDateStop.getText().toString());
                                map.put("operator", name);
                                map.put("faultContent", faultContent.getText().toString());
                                map.put("factory", factory);
                                return map;
                            }
                        };
                        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                        queue.add(stringRequest);
                    } else {
                        // 填写后半部分信息
                        if (editTextDateStart.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写维修开始时间", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (editDateStop.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写维修结束时间", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (servicemanInCharge.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写维修负责人", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (serviceman.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写维修人员", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (faultReason.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写故障原因分析", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (serviceMethod.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写维修方法", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (proposer.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写方法提出人", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (editDateRestore.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写故障恢复时间", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (inspector.getText().toString().equals("")) {
                            Toast.makeText(this, "请填写检查人", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        String url = Config.url + "/sjq/SolidliquidProduceFault/addAfterPart";
                        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            String code = jsonObject.getString("code");

                                            if (code.startsWith("2")) {
                                                Toast.makeText(guzhangjilu7_7.this,
                                                        "提交成功", Toast.LENGTH_SHORT).show();
                                            }
                                            else
                                                Toast.makeText(guzhangjilu7_7.this,
                                                        "提交失败", Toast.LENGTH_SHORT).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(guzhangjilu7_7.this,
                                        "提交失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() {
                                Map<String, String> map=new HashMap<>();
                                map.put("faultId", faultId.toString());
                                map.put("serviceTimeStart", editTextDateStart.getText().toString());
                                map.put("serviceTimeEnd", editTextDateEnd.getText().toString());
                                map.put("servicemanInCharge", servicemanInCharge.getText().toString());
                                map.put("serviceman", serviceman.getText().toString());
                                map.put("faultReason", faultReason.getText().toString());
                                map.put("serviceMethod", serviceMethod.getText().toString());
                                map.put("proposer", proposer.getText().toString());
                                map.put("recoverTime", editDateRestore.getText().toString());
                                map.put("inspector", inspector.getText().toString());
                                map.put("factory", factory);
                                return map;
                            }
                        };
                        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                        queue.add(stringRequest);
                    }
                }).show();
    }

    /*
    public void save(View view) {
        if(editDateStop.getText().toString().equals("")) {
            Toast.makeText(this, "请填写停机时间", Toast.LENGTH_SHORT).show();
            return;
        }
        if(operator.getText().toString().equals("")) {
            Toast.makeText(this, "请填写操作人", Toast.LENGTH_SHORT).show();
            return;
        }
        if (faultContent.getText().toString().equals("")) {
            Toast.makeText(this, "请填写故障情况描述", Toast.LENGTH_SHORT).show();
            return;
        }
        if (editTextDateStart.getText().toString().equals("")) {
            Toast.makeText(this, "请填写维修开始时间", Toast.LENGTH_SHORT).show();
            return;
        }
        if (editTextDateEnd.getText().toString().equals("")) {
            Toast.makeText(this, "请填写维修结束时间", Toast.LENGTH_SHORT).show();
            return;
        }
        if (servicemanInCharge.getText().toString().equals("")) {
            Toast.makeText(this, "请填写维修负责人", Toast.LENGTH_SHORT).show();
            return;
        }
        if (serviceman.getText().toString().equals("")) {
            Toast.makeText(this, "请填写维修人员", Toast.LENGTH_SHORT).show();
            return;
        }
        if (faultReason.getText().toString().equals("")) {
            Toast.makeText(this, "请填写故障原因分析", Toast.LENGTH_SHORT).show();
            return;
        }
        if (serviceMethod.getText().toString().equals("")) {
            Toast.makeText(this, "请填写维修方法", Toast.LENGTH_SHORT).show();
            return;
        }
        if (proposer.getText().toString().equals("")) {
            Toast.makeText(this, "请填写方法提出人", Toast.LENGTH_SHORT).show();
            return;
        }
        if (editDateRestore.getText().toString().equals("")) {
            Toast.makeText(this, "请填写故障恢复时间", Toast.LENGTH_SHORT).show();
            return;
        }
        if (inspector.getText().toString().equals("")) {
            Toast.makeText(this, "请填写检查人", Toast.LENGTH_SHORT).show();
            return;
        }
        new AlertDialog.Builder(this)
                .setTitle("确定保存吗？")
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) ->
                        Toast.makeText(this, "取消保存", Toast.LENGTH_SHORT).show()))
                .setPositiveButton("确定", (dialog, which) -> {
                    //提交请求
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url = Config.url + "/sjq/SolidliquidProduceFault/add";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    Log.d("jy", response);
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");
                                        if (code.startsWith("2"))
                                            Toast.makeText(guzhangjilu7_7.this,
                                                    "保存成功", Toast.LENGTH_SHORT).show();
                                        else
                                            Toast.makeText(guzhangjilu7_7.this,
                                                    "保存失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(guzhangjilu7_7.this,
                                    "2保存失败" + error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> map=new HashMap<>();
                            map.put("fillTime", nowDate);
                            map.put("faultTime", editDateStop.getText().toString());
                            map.put("serviceTimeStart", editTextDateStart.getText().toString());
                            map.put("serviceTimeEnd", editTextDateEnd.getText().toString());
                            map.put("recoverTime", editDateRestore.getText().toString());
                            //map.put("equipmentName", equipmentName.getText().toString());
                            map.put("operator",operator.getText().toString());
                            map.put("faultContent", faultContent.getText().toString());
                            map.put("servicemanInCharge", servicemanInCharge.getText().toString());
                            map.put("serviceman", serviceman.getText().toString());
                            map.put("faultReason", faultReason.getText().toString());
                            map.put("serviceMethod", serviceMethod.getText().toString());
                            map.put("inspector", inspector.getText().toString());
                            map.put("proposer", proposer.getText().toString());
                            return map;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                })
                .show();
    }*/



    @SuppressLint({"DefaultLocale", "ResourceType"})
    public void chooseStopDate(View view) {
        Calendar calendar=Calendar.getInstance();
        int YEAR = calendar.get(Calendar.YEAR);
        int MONTH = calendar.get(Calendar.MONTH);
        int DAY = calendar.get(Calendar.DAY_OF_MONTH);
        int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
        int MINUTE = calendar.get(Calendar.MINUTE);
        new DatePickerDialog(this, 2, (view1, year, month, day) -> {
            // 更新EditText控件日期 小于10加0
            new TimePickerDialog(this, 2, (view11, hourOfDay, minute) -> {
                //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                editDateStop.setText(new StringBuilder()
                        .append(year)
                        .append("/")
                        .append((month + 1) < 10 ? "0"
                                + (month + 1) : (month + 1))
                        .append("/")
                        .append((day < 10) ? "0" + day : day)
                        .append(" ")
                        .append(String.format("%02d",hourOfDay))
                        .append(":")
                        .append(String.format("%02d",minute)));
            },
                    HOUR, MINUTE, true).show();
        },
                YEAR, MONTH, DAY).show();
    }

    @SuppressLint({"DefaultLocale", "ResourceType"})
    public void chooseStartDate(View view) {
        Calendar calendar=Calendar.getInstance();
        int YEAR = calendar.get(Calendar.YEAR);
        int MONTH = calendar.get(Calendar.MONTH);
        int DAY = calendar.get(Calendar.DAY_OF_MONTH);
        int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
        int MINUTE = calendar.get(Calendar.MINUTE);
        new DatePickerDialog(this, 2, (view1, year, month, day) -> {
            // 更新EditText控件日期 小于10加0
            new TimePickerDialog(this, 2, (view11, hourOfDay, minute) -> {
                //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                editTextDateStart.setText(new StringBuilder()
                        .append(year)
                        .append("/")
                        .append((month + 1) < 10 ? "0"
                                + (month + 1) : (month + 1))
                        .append("/")
                        .append((day < 10) ? "0" + day : day)
                        .append(" ")
                        .append(String.format("%02d",hourOfDay))
                        .append(":")
                        .append(String.format("%02d",minute)));
            }, HOUR, MINUTE, true).show();

        }, YEAR, MONTH, DAY).show();
    }

    @SuppressLint({"DefaultLocale", "ResourceType"})
    public void chooseEndDate(View view) {
        Calendar calendar=Calendar.getInstance();
        int YEAR = calendar.get(Calendar.YEAR);
        int MONTH = calendar.get(Calendar.MONTH);
        int DAY = calendar.get(Calendar.DAY_OF_MONTH);
        int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
        int MINUTE = calendar.get(Calendar.MINUTE);
        new DatePickerDialog(this, 2, (view1, year, month, day) -> {
            // 更新EditText控件日期 小于10加0
            new TimePickerDialog(this, 2, (view11, hourOfDay, minute) -> {
                //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                editTextDateEnd.setText(new StringBuilder()
                        .append(year)
                        .append("/")
                        .append((month + 1) < 10 ? "0"
                                + (month + 1) : (month + 1))
                        .append("/")
                        .append((day < 10) ? "0" + day : day)
                        .append(" ")
                        .append(String.format("%02d",hourOfDay))
                        .append(":")
                        .append(String.format("%02d",minute)));
            }, HOUR, MINUTE, true).show();

        }, YEAR, MONTH, DAY).show();
    }

    @SuppressLint({"DefaultLocale", "ResourceType"})
    public void chooseRestoreDate(View view) {
        Calendar calendar=Calendar.getInstance();
        int YEAR = calendar.get(Calendar.YEAR);
        int MONTH = calendar.get(Calendar.MONTH);
        int DAY = calendar.get(Calendar.DAY_OF_MONTH);
        int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
        int MINUTE = calendar.get(Calendar.MINUTE);
        new DatePickerDialog(this, 2, (view1, year, month, day) -> {
            // 更新EditText控件日期 小于10加0
            new TimePickerDialog(this, 2, (view11, hourOfDay, minute) -> {
                //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                editDateRestore.setText(new StringBuilder()
                        .append(year)
                        .append("/")
                        .append((month + 1) < 10 ? "0"
                                + (month + 1) : (month + 1))
                        .append("/")
                        .append((day < 10) ? "0" + day : day)
                        .append(" ")
                        .append(String.format("%02d",hourOfDay))
                        .append(":")
                        .append(String.format("%02d",minute)));
            }, HOUR, MINUTE, true).show();

        }, YEAR, MONTH, DAY).show();
    }

}